package cn.geoportal.benoserver.service.impl;

import cn.geoportal.benoserver.entity.PoiInfo;
import cn.geoportal.benoserver.service.PoiInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.geoportal.benoserver.mapper.PoiInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class PoiInfoServiceImpl extends ServiceImpl<PoiInfoMapper, PoiInfo> implements PoiInfoService {

    @Autowired
    private PoiInfoMapper poiInfoMapper;

    @Override
    public int addPoiInfo(String name, String address, String phone, double lat, double lng) {
        String geomStr = "POINT" + "(" + lng + " " + lat + ")";
        QueryWrapper<PoiInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        if(poiInfoMapper.selectCount(wrapper)>0){
            return 0;
        }
        return poiInfoMapper.addPoiInfo(name, address, phone, lat, lng, geomStr);
    }

    @Override
    public IPage<PoiInfo> queryPoiInfoPage(Page<PoiInfo> page, QueryWrapper<PoiInfo> wrapper) {
        return poiInfoMapper.queryPoiInfoPage(page, wrapper);
    }

    @Override
    public PoiInfo getPoiInfoById(Integer id) {
        return poiInfoMapper.getPoiInfoById(id);
    }

    @Override
    public int updatePoiInfoById(Integer id, String name, String address, String phone, double lat, double lng) {
        String geomStr = "POINT" + "(" + lng + " " + lat + ")";
        QueryWrapper<PoiInfo> wrapper = new QueryWrapper<>();
        wrapper.ne("id", id).eq("name", name);
        if(poiInfoMapper.selectCount(wrapper)>0){
            return -1;
        }
        return poiInfoMapper.updatePoiInfoById(id, name, address, phone, lat, lng, geomStr);
    }

    @Override
    public int deletePoiById(Integer id) {
        return poiInfoMapper.deleteById(id);
    }

    @Override
    public List<PoiInfo> findWithDistance(double lat, double lng, double radius) {
        String pt = "POINT" + "(" + lng + " " + lat + ")";
        return poiInfoMapper.findDistanceWithIn(pt, radius);
    }

    @Override
    public List<PoiInfo> findNearestPOI(double lat, double lng, Integer count) {
        String pt = "POINT" + "(" + lng + " " + lat + ")";
        return poiInfoMapper.findNearestPOI(pt, count);
    }
}




